#!/usr/bin/env python
# encoding: utf-8
'''
@author: qiuyan
@contact: winston@peipeiyun.com
@file: wap_server.py
@time: 20120/2/13 6:27 PM
@desc:
'''
from conf import conf
from sanic.response import html,text
from core import app,template
from core.temp_render import render
from core.health import health_bp
app.blueprint(health_bp)

from apps.urls import group_bp
app.blueprint(group_bp)


from middleware.auth import authorized


# @group_bp.middleware('request')
# @authorized
# async def login_require(request):
#     print('---------------login required')


#
# @app.route("/")
# async def index(request):
#
#     try:
#         content = render('index.html')
#         return html(content)
#     except:
#         return text('/')
#
# @app.route('/static/<filename:path>')
# async def static(request, filename):
#     return text(filename)
#
#
# @app.route('/<path:[^/].*?>')
# async def slash(request, path):
#     return text(path)
#
#
# @app.route('/<name:.*>')
# async def noslash(request, name):
#     return text(name)

import sys

from signal import signal, SIGINT
import asyncio
import uvloop

if __name__ == '__main__':
    port = conf.port
    if sys.platform=='win32':
        app.run(host="0.0.0.0", port=port, debug=True)
    else:
        asyncio.set_event_loop(uvloop.new_event_loop())
        server = app.create_server(host="0.0.0.0", port=port, return_asyncio_server=True,access_log=True)
        loop = asyncio.get_event_loop()
        task = asyncio.ensure_future(server)
        signal(SIGINT, lambda s, f: loop.stop())
        try:
            loop.run_forever()
        except:
            loop.stop()
